
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;
const int N = 25;       
const char ChessBoard = ' ';  
const char flag1 = 'o';    
const char flag2 = 'x';   

 struct Position{    
    int row;        
    int col;        
};

class GoBang{     
public:
    GoBang(){
        InitChessBoard();      
    }
    void Play(){      
        Position Play1;   
        Position Play2;   
        while (1){
            int mode = ChoiceMode();
            while (1){
                if (mode == 1){               
                    PlayChess(Play1, 1, flag1);         
                    if (GetVictory(Play1, 1, flag1)){       
                        break;
                    }
                    PlayChess(Play2, 2, flag2);         
                    if (GetVictory(Play2, 2, flag2)){       
                        break;
                    }
                }
            }
            cout << "======再来一局=======" << endl;
            cout << "yes or no :";  
            char s[] = "yes";
            cin >> s;
            if (strcmp(s, "no") == 0){
                break;
            }
        }
    }

protected:
    void InitChessBoard(){          
        for (int i = 0; i < N + 1; ++i){
            for (int j = 0; j < N + 1; ++j){
                _ChessBoard[i][j] = ChessBoard;
            }
        }
    }

    int ChoiceMode(){           
       
        InitChessBoard();       
        cout << "*************************************************" << endl;
        cout << "******************0、退出************************" << endl;
        cout << "******************1、玩家VS玩家******************" << endl;
        cout << "*************************************************" << endl;
        while (1){
            int i = 0;
            cout << "请选择模式：";
            cin >> i;
            if (i == 0){       
                exit(1);
            }
            else if (i == 1){
                return i;
            }
            else{
                cout << "非法输入，请重新输入！" << endl;
            }
        }
    }

    void PrintChessBoard(){        
    
        printf("     1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25\n");
        printf("   |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");
        for (int i = 1; i < N + 1; ++i)
        {
            printf("%2d ", i);
            printf("| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |\n", _ChessBoard[i][1], _ChessBoard[i][2], _ChessBoard[i][3], _ChessBoard[i][4], _ChessBoard[i][5],
			 _ChessBoard[i][6], _ChessBoard[i][7], _ChessBoard[i][8], _ChessBoard[i][9], _ChessBoard[i][10],
			 _ChessBoard[i][11], _ChessBoard[i][12], _ChessBoard[i][13], _ChessBoard[i][14], _ChessBoard[i][15],
			 _ChessBoard[i][16], _ChessBoard[i][17], _ChessBoard[i][18], _ChessBoard[i][19], _ChessBoard[i][20],
			 _ChessBoard[i][21], _ChessBoard[i][22], _ChessBoard[i][23], _ChessBoard[i][24], _ChessBoard[i][25]);
            printf("   |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");
        }
        cout << endl;
    }

    void PlayChess(Position& pos, int player, char flag){
        PrintChessBoard();      
        while (1){
            printf("请玩家%d输入坐标：", player);
            cin >> pos.row >> pos.col;
            if (JudgeValue(pos) == 1){        //判断坐标是否合法
                break;
            }
            cout << "坐标不合法，请重新输入：" << endl;
        }
        _ChessBoard[pos.row][pos.col] = flag;
    }

    int JudgeValue(const Position& pos){       
        if (pos.row > 0 && pos.row <= N && pos.col > 0 && pos.col <= N){
            
            if (_ChessBoard[pos.row][pos.col] == ChessBoard){
                return 1;      
            } 
        }
        return 0;       
    }

    int JudgeVictory(Position pos, char flag){     
        int begin = 0;
        int end = 0;

        //行
        (pos.col - 5) > 0 ? begin = (pos.col - 5) : begin = 1;
        (pos.col + 5) > N ? end = N : end = (pos.col + 5);
        for (int i = pos.row, j = begin; j + 5 <= end; ++j){
            if (_ChessBoard[i][j] == flag && _ChessBoard[i][j + 1] == flag &&
                _ChessBoard[i][j + 2] == flag && _ChessBoard[i][j + 3] == flag &&
                _ChessBoard[i][j + 4] == flag&& _ChessBoard[i][j + 5] == flag)
                return 1;
        }
        //列
        (pos.row - 5) > 0 ? begin = (pos.row - 5) : begin = 1;
        (pos.row + 5) > N ? end = N : end = (pos.row + 5);
        for (int j = pos.col, i = begin ; i + 5 <= end; ++i){
            if (_ChessBoard[i][j] == flag && _ChessBoard[i + 1][j] == flag &&
                _ChessBoard[i + 2][j] == flag && _ChessBoard[i + 3][j] == flag &&
                _ChessBoard[i + 4][j] == flag && _ChessBoard[i + 5][j] == flag)
                return 1;
        }
        //主对角线
        int len = 0;    
        int start = 0;
        int finish = 0;
        pos.row > pos.col ? len = pos.col - 1 : len = pos.row - 1;
        if (len > 5){
            len = 5;
        }
        begin = pos.row - len;       
        start = pos.col - len;       

        pos.row > pos.col ? len = N - pos.row : len = N - pos.col;
        if (len > 5){
            len = 5;
        }
        end = pos.row + len;         
        finish = pos.col + len;      

        for (int i = begin, j = start; (i + 5 <= end) && (j + 5 <= finish); ++i, ++j){
            if (_ChessBoard[i][j] == flag && _ChessBoard[i + 1][j + 1] == flag &&
                _ChessBoard[i + 2][j + 2] == flag && _ChessBoard[i + 3][j + 3] == flag &&
                _ChessBoard[i + 4][j + 4] == flag && _ChessBoard[i + 5][j + 5] == flag)
                return 1;
        }
        //副对角线
        (pos.row - 1) > (N - pos.col) ? len = N - pos.col : len = pos.row - 1;
        if (len > 5){
            len = 5;
        }
        begin = pos.row - len;       
        start = pos.col + len;       

        (N - pos.row) > (pos.col - 1) ? len = pos.col - 1 : len = N - pos.row;
        if (len > 5){
            len = 5;
        }
        end = pos.row + len;         
        finish = pos.col - len;      
        for (int i = begin, j = start; (i + 5 <= end) && (j - 5 >= finish); ++i, --j){
            if (_ChessBoard[i][j] == flag && _ChessBoard[i + 1][j - 1] == flag &&
                _ChessBoard[i + 2][j - 2] == flag && _ChessBoard[i + 3][j - 3] == flag &&
                _ChessBoard[i + 4][j - 4] == flag && _ChessBoard[i + 5][j - 5] == flag)
                return 1;
        }
        
        for (int x = 1; x < N + 1; ++x){
            for (int y = 1; y < N + 1; ++y){
                if (_ChessBoard[x][y] == ChessBoard){
                    return 0;       
                }
            }
        }
        return -1;      //和局
    }

    bool GetVictory(Position& pos, int player, char flag){       
        if (JudgeVictory(pos, flag) != 0){    
            if (JudgeVictory(pos, flag) == 1){     
                PrintChessBoard();     
                    printf("恭喜玩家%d获胜！\n", player);
            }
            else{
                printf("和局！\n");
            }
            return true;      
        }   
        return false;         
    }

private:
    char _ChessBoard[N + 1][N + 1];    
};

int main(){
    GoBang g;
    g.Play();
    return 0;
}
